package zstack

import (
	"bc/util"
	"encoding/json"
	"fmt"
)

type instanceOffering struct {

}

func (this *instanceOffering) Create (m map[string]interface{},OAuth string) (string,error) {

	b := map[string]interface{}{
		"params":map[string]interface{}{
			"name":m["name"],
			"cpuNum": m["cpu_num"],
			"memorySize" : m["memory_size"],
			"sortKey": m["sort_key"],
			"type": m["type"],
		},
		"systemTags":[]interface{}{},
		"userTags":[]interface{}{},
	}
	jsonStr,_ := json.Marshal(b)

	oauth := "OAuth " + OAuth
	result,err := util.Curl.
		Header(map[string]string{
			"Authorization":oauth,
			"Content-Type":"application/json;charset=UTF-8",
			"X-Web-Hook":util.Config.Zstack.WebHook + "instance_offer_call_back",
		}).
		Body(string(jsonStr)).
		Url(util.Config.Zstack.ApiUrl + "v1/instance-offerings").
		POST()

	return result,err

}
func (this *instanceOffering) Delete(uuid string,oauth string) (string,error) {

	url := fmt.Sprintf("v1/instance-offerings/%s?deleteMode=%s",uuid,"Permissive")

	result,err := util.Curl.Header(map[string]string{
		"Content-Type":"application/json;charset=UTF-8",
		"Authorization":"OAuth " + oauth,
		"X-Web-Hook":util.Config.Zstack.WebHook + "instance_offer_del_call_back?uuid=" + uuid,
	}).Url(util.Config.Zstack.ApiUrl + url).DELETE()

	return result,err
}
func (this *instanceOffering) Update(body string,uuid string,oauth string) (string,error){
	url := fmt.Sprintf("v1/instance-offerings/%s/actions",uuid)
	result ,err := util.Curl.Header(map[string]string{
		"Content-Type":"application/json;charset=UTF-8",
		"Authorization":"OAuth " + oauth,
		"X-Web-Hook":util.Config.Zstack.WebHook + "instance_offer_update_call_back",
	}).Body(body).Url(util.Config.Zstack.ApiUrl + url).PUT()
	return result,err
}
/**
	更新云主机计算规格的启用状态
 */
func (this *instanceOffering)ChangeInstanceOfferingState(uuid string,body string) (string , error){

	oauth,err := Oauth()
	if err != nil{
		panic(err)
	}
	url := fmt.Sprintf("v1/instance-offerings/%s/actions",uuid)
	url = util.Config.Zstack.ApiUrl + url
	header := make(map[string]string)
	header["Content-Type"] = "application/json;charset=UTF-8"
	header["Authorization"] = "OAuth " + oauth
	header["X-Web-Hook"] = util.Config.Zstack.WebHook + "change_instance_offering_state_call_back?uuid="+uuid

	result,err := util.Curl.Header(header).Body(body).Url(url).PUT()
	return result,err

}
var InsOffer = &instanceOffering{}
